package algotradingfx.backtesting;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;

import algotradingfx.data.TickReader;
import algotradingfx.utils.Constants;
import atp.client.trading.methods.Forex;
import atp.client.trading.strategies.StrategyPush;
import atp.commons.util.Tick;

public class BackTester {

	private final Forex forex;
	private final TickReader reader;
	private final StrategyPush strategy;
	private Tick lastTick;

	public BackTester(Reader dataSource, StrategyPush strategy) throws FileNotFoundException {
		this.reader = new TickReader(dataSource);
		this.forex = new Forex(null, Constants.SSID, strategy.getInfo().getName());
		this.strategy = strategy;
		this.strategy.forex = this.forex;
	}

	public void run() throws IOException {
		while ((lastTick = reader.toTick(reader.readLine())) != null) {
			strategy.run(lastTick);
		}
	}

	public void runOnce() throws IOException {
		if (lastTick == null) {
			lastTick = reader.toTick(reader.readLine());
		}
		strategy.run(lastTick);
	}
}
